package container

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/Unixeno/islands/cmd/connection"
	"github.com/Unixeno/islands/cmd/utils"
	"github.com/Unixeno/islands/pb"
	"github.com/spf13/cobra"
)

func GetContainerInspectCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "inspect id",
		Short: "inspect the container",
		Long:  `inspect specific container in your islands`,
		Args:  cobra.MinimumNArgs(1),
		Run:   containerInspectHandle,
	}
}

func containerInspectHandle(cmd *cobra.Command, args []string) {
	id := CheckNameOrId(args[0])
	resp, err := connection.Client.InspectContainer(context.Background(), &pb.ContainerID{
		Id: id,
	})

	if err != nil {
		fmt.Printf("Cannot inspect the container for reason %v\n", err)
		return
	} else {
		if !utils.PrintError(resp.Err) {
			jsonData, err := json.MarshalIndent(resp.Info, "", "\t")
			if err != nil {
				fmt.Println(err)
			} else {
				fmt.Println(string(jsonData))
			}
		}
	}
}
